import 'package:flexible/flexible.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_1_25_0/testWidget/pb/common/model.pb.dart';
import 'package:flutter_app_1_25_0/testWidget/pb/exercise_practise/model.pb.dart';
import 'package:logging/logging.dart';

Logger _log = Logger('Exercise');

/*
 * Create by weslywang on 2020/8/21
 * Copyright © Tencent
 *
 * Description: 基本数据项显示组件
 */

DataItemType getDataItemType(DataItem item) {
  DataItemType type = DataItemType.data_item_text;
  if (item.image.url.isNotEmpty) {
    type = DataItemType.data_item_image;
  } else if (item.audio.url.isNotEmpty) {
    type = DataItemType.data_item_audio;
  } else if (item.video.url.isNotEmpty) {
    type = DataItemType.data_item_video;
  } else if (item.text.name.isNotEmpty) {
    type = DataItemType.data_item_text;
  }
  return type;
}

class DataItemWidget extends StatelessWidget {
  DataItemWidget({@required this.dataItem, this.prefix = '', this.maxLines, this.isTextHighlight = false});

  final DataItem dataItem;
  final String prefix;
  final int maxLines;
  final bool isTextHighlight; //文本是否需要高亮

  Widget _buildText(BuildContext context) {
    // 输出带转义字符的字符串变量
    // print(json.encode(dataItem.text.name));
    double fontSize = 12.0;
    String prefix = '';
    if (dataItem.text.style == TextItemStyle.text_item_style_style1) {
      prefix = this.prefix;
    }
    Color color = Colors.black;

    return Container(
      width: double.infinity,
      // color: Colors.redAccent,
      // alignment: Alignment.topLeft,
      child: Text(
        '$prefix${dataItem.text.name ?? ""}',
        maxLines: maxLines,
        overflow: maxLines != null ? TextOverflow.ellipsis : TextOverflow.clip,
        style: TextStyle(fontSize: flexible(context, fontSize), color: color),
      ),
    );
  }

  Widget _buildImage(BuildContext context) {
    return Container(
      width: double.infinity,
    );
  }

  Widget _buildAudio(BuildContext context) {
    return Container(
      width: double.infinity,
    );
  }

  Widget _buildVideo(BuildContext context) {
    return Container(
      width: double.infinity,
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget widget = Container();
    DataItemType dataItemType = getDataItemType(dataItem);
//    _log.fine('DataItemType数据类型：$dataItemType, $dataItem');
    switch (dataItemType) {
      case DataItemType.data_item_text:
        widget = _buildText(context);
        break;
      case DataItemType.data_item_image:
        widget = _buildImage(context);
        break;
      case DataItemType.data_item_audio:
        widget = _buildAudio(context);
        break;
      case DataItemType.data_item_video:
        widget = _buildVideo(context);
        break;
    }

    return widget;
  }
}

